package com.example.shopping.controller;

import com.example.shopping.pojo.TypeTemplate;
import com.example.shopping.service.TypeTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

/*
Create by 苍狼 于 2020/1/16
站在巅峰看世界，
落在深渊思人生。
*/
@Controller
//跨域注解
@CrossOrigin
public class TypeTemplateController {
    private int specId;
    @Autowired(required = false)
    private TypeTemplateService typeTemplateService;

    //全部查询
    @RequestMapping("/selectTypeLateList")
    public String selectTypeLateList(HttpServletRequest request) {
        List<TypeTemplate> typeTemplates = typeTemplateService.selectTypeTempLateList();
        request.getSession().setAttribute("typeTemplates", typeTemplates);
        return "typeLateList";
    }

    @RequestMapping("/insertTypeTemplate")
    public String insertTypeTemplate(TypeTemplate typeTemplate) {
        int i = typeTemplateService.insertTypeTemplate(typeTemplate);
        if (i > 0) {
            return "redirect:selectTypeLateList";
        }
        return "redirect:insertTypeTemplate";
    }

    //删除
    @RequestMapping("/deleteTypeTemplate")
    public String deleteTypeTemplate(int id) {
        int i = typeTemplateService.deleteTypeTemplate(id);
        if (i > 0) {
            return "redirect:selectTypeLateList";
        }
        return "redirect:deleteTypeTemplate";
    }

    //模糊查询
    @RequestMapping("/selectTypeLateListByLike")
    public String selectTypeLateListByLike(String name, HttpServletRequest request) {
        List<TypeTemplate> typeTemplates = typeTemplateService.selectTypeLateListByLike(name);
        request.getSession().setAttribute("typeTemplates", typeTemplates);
        return "typeLateList";
    }

    //修改页面
    @RequestMapping("/selectTypeLateById")
    public String selectTypeLateById(int id, HttpServletRequest request) {
        TypeTemplate typeTemplate = typeTemplateService.selectTypeLateById(id);
        request.getSession().setAttribute("typeTemplate", typeTemplate);
        return "updateTypeTemplate";
    }

    //修改方法
    @RequestMapping("/updateTypeLateById")
    public String updateTypeLateById(TypeTemplate typeTemplate) {
        int i = typeTemplateService.updateTypeLateById(typeTemplate);
        if (i > 0) {
            return "redirect:selectTypeLateList";
        }
        return "redirect:updateTypeLateById";
    }

}
